--[[
	This file is part of FlexBar3.

	FlexBar3 is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	FlexBar3 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with FlexBar3.  If not, see <http://www.gnu.org/licenses/>.
]]

local Button = FlexBar3:GetModule("Button");
local SecureStateDriverFrame = Button.SecureStateDriverFrame;
local ItemClass = Button:NewButtonClass("AttributeItem", 1);

function ItemClass:PlaceItem(Itemlink, StateModifier)
	self:SetModifiedAttribute("type", "item", StateModifier);
	self:SetModifiedAttribute("type-RightButton", "item", StateModifier);
	self:SetModifiedAttribute("type-MiddleButton", "item", StateModifier);
	local ItemAttribute = ("itemlink:%s"):format(Itemlink);
	self:SetModifiedAttribute("item", ItemAttribute, StateModifier);
	self:SetModifiedAttribute("item-RightButton", ItemAttribute, StateModifier);
	self:SetModifiedAttribute("item-MiddleButton", ItemAttribute, StateModifier);
	self:TriggerCallback("ActionChanged");
end

function ItemClass:DragItemOff()
	local Item = self:GetModifiedAttribute("item");
	if(Item) then
		Item = Item:sub(10);
		if(Item ~= "") then
			self:PickupAction("item", Item);
			self:SetModifiedAttribute("type", nil);
			self:SetModifiedAttribute("item", nil);
			local CurrentStateDB = self.StateDB[self:GetModifiedState()];
			CurrentStateDB.Type = nil;
			CurrentStateDB.Item = nil;
			self:TriggerCallback("ActionChanged");
		end
	end
end

function ItemClass:RefreshItemTexture()
	local Item = self:GetModifiedAttribute("item");
	if(Item) then
		Item = Item:sub(10);
		if(Item ~= "") then
			local ItemTexture = GetItemIcon(Item);
			if(ItemTexture) then
				self:SetTexture(ItemTexture);
			else
				self:SetTexture([[Interface/Icons/INV_Misc_QuestionMark]]);
			end
		else
			self:SetTexture(nil);
		end
	else
		self:SetTexture(nil);
	end
end
